package com.samphanie.auiu.biz_home.fragment.home

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.samphanie.auiu.biz_home.databinding.LayoutHomeOpGridItemBinding
import com.samphanie.auiu.common.route.AuiuRoute
import com.samphanie.auiu.library.utils.AuiuDisplayUtil
import com.samphanie.auiu.biz_home.model.SubCategory
import com.samphanie.auiu.ui.item.AuiuDataItem
import com.samphanie.auiu.ui.item.AuiuViewHolder
import kotlinx.android.extensions.LayoutContainer

/**
 * @author ZSY
 */
class GridItem(val list: List<SubCategory>) :
    AuiuDataItem<List<SubCategory>, AuiuViewHolder>(list) {

    override fun onBindData(holder: AuiuViewHolder, position: Int) {
        val context = holder.itemView.context
        val gridView = holder.itemView as RecyclerView
        gridView.adapter = GridAdapter(context, list)
    }

    override fun getItemView(parent: ViewGroup): View? {
        val gridView = RecyclerView(parent.context)
        val params = RecyclerView.LayoutParams(
            RecyclerView.LayoutParams.MATCH_PARENT,
            RecyclerView.LayoutParams.WRAP_CONTENT
        )

        params.bottomMargin = AuiuDisplayUtil.dp2px(10f)

        gridView.layoutManager = GridLayoutManager(parent.context, 4)
        gridView.layoutParams = params
        gridView.setBackgroundColor(Color.WHITE)
        return gridView
    }

    inner class GridAdapter(val context: Context, val list: List<SubCategory>) :
        RecyclerView.Adapter<HMyViewHolder>() {
        private var inflater = LayoutInflater.from(context)
        private lateinit var binding: LayoutHomeOpGridItemBinding

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HMyViewHolder {
            binding = LayoutHomeOpGridItemBinding.inflate(inflater, null, false)
            return HMyViewHolder(binding.root)
        }

        override fun getItemCount(): Int {
            return list.size
        }

        override fun onBindViewHolder(holder: HMyViewHolder, position: Int) {
            val subcategory = list[position]
            binding.subCategory = subcategory

            // binding.itemImage.loadUrl(subcategory.subCategoryIcon)
            // binding.itemTitle.text = subcategory.subCategoryName

            holder.itemView.setOnClickListener {
                // 会跳转到子分类列表上面去，，是一个单独的页面
                // Toast.makeText(context, "you touch me: GridItem-" + position, Toast.LENGTH_SHORT).show()
                val bundle = Bundle()
                bundle.putString("cateId", subcategory.cateId)
                bundle.putString("subCateId", subcategory.id)
                bundle.putString("cateTitle", subcategory.subCategoryName)
                AuiuRoute.startActivity(context, bundle, AuiuRoute.Destination.GOODS_LIST)
            }
        }
    }

    class HMyViewHolder(val view: View) : RecyclerView.ViewHolder(view), LayoutContainer {
        override val containerView: View?
            get() = view
    }

}